package cn.maiyar.mybatis;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.maiyar.common.constant.CommonConstant;
import cn.maiyar.common.entity.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 条件构造器
 *
 * @author c.c
 */
public class Condition {

    // 默认的起始页数
    private final static int DEFAULT_PAGE = 1;
    // 默认的每页数量
    private final static int DEFAULT_SIZE = 8;

    /**
     * 匹配排序关键字
     *
     * @param param
     * @param sortKey
     * @return
     */
    public static String matching(String param, List<String> sortKey) {
        if (sortKey == null || StrUtil.isEmpty(param) || CollUtil.isEmpty(sortKey)) {
            return null;
        }
        String[] ascsArray = param.split(StringPool.COMMA);
        return Arrays.stream(ascsArray)
                .filter(sortKey::equals)
                .collect(Collectors.joining(StringPool.COMMA));
    }

    /**
     * 转化成mybatis plus中的Page
     *
     * @param query 页码参数
     * @param <T>   返回对象
     * @return
     */
    public static <T> Page<T> getPage(Query query) {
        if (query == null) {
            query = new Query();
        }
        Integer page = query.getPage();
        Page<T> pages = new Page<>(page != null ? page : DEFAULT_PAGE,
                query.getSize() != null ? query.getSize() : DEFAULT_SIZE);
        List<OrderItem> orderItems = new ArrayList<>();
        String ascs = query.getAscs();
        if (StrUtil.isNotEmpty(ascs)) {
            orderItems.addAll(OrderItem.ascs(SqlKeyword.filter(ascs).split(StringPool.COMMA)));
        }
        String descs = query.getDescs();
        if (StrUtil.isNotEmpty(descs)) {
            orderItems.addAll(OrderItem.descs(SqlKeyword.filter(descs).split(StringPool.COMMA)));
        }
        if (!orderItems.isEmpty()) {
            pages.addOrder(orderItems);
        }
        return pages;
    }

    /**
     * 获取mybatis plus中的QueryWrapper
     *
     * @param entity
     * @param <T>
     * @return
     */
    public static <T> QueryWrapper<T> getQueryWrapper(T entity) {
        return new QueryWrapper<>(entity);
    }

    /**
     * 获取mybatis plus中的QueryWrapper
     *
     * @param query 查询条件
     * @param clazz 实体类
     * @param <T>   类型
     * @return QueryWrapper
     */
    public static <T> QueryWrapper<T> getQueryWrapper(Map<String, Object> query, Class<T> clazz) {
        Map<String, Object> exclude = Map.of(
                CommonConstant.DEFAULT_TENANT_ID_NAME, CommonConstant.DEFAULT_TENANT_ID_PARAM,
                "page", "page",
                "size", "size",
                "ascs", "ascs",
                "descs", "descs"
        );
        return getQueryWrapper(query, exclude, clazz);
    }

    /**
     * 获取mybatis plus中的QueryWrapper
     *
     * @param query   查询条件
     * @param exclude 排除的查询条件
     * @param clazz   实体类
     * @param <T>     类型
     * @return QueryWrapper
     */
    public static <T> QueryWrapper<T> getQueryWrapper(Map<String, Object> query, Map<String, Object> exclude, Class<T> clazz) {
        exclude.forEach((k, v) -> query.remove(k));
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(BeanUtils.instantiateClass(clazz));
        SqlKeyword.buildCondition(query, queryWrapper);
        return queryWrapper;
    }
}
